import Vue from 'vue'
import Router from 'vue-router'
import NavMenu from '../components/NavMenu/navMenu'
import Login from '../components/Login/login'
import store from '@/store'
import interfaceList from '../utils/interface'

import {
  MessageBox
} from 'element-ui'
Vue.prototype.$confirm = MessageBox;

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      name: 'NavMenu',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          next()
        }
      },
      children: [{
        path: '/home',
        component: () => import('../views/home')
      }]
    },
    {
      path: '/sales',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          next()
        }
      },
      children: [{
        path: '/sales/salesDetail',
        component: () => import('../views/sales')
      }]
    },
    {
      path: '/cuisine',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          next()
        }
      },
      children: [{
        path: '/cuisine/cuisineList',
        component: () => import('@/views/cuisine')
      }]
    },
    {
      path: '/customer',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          interfaceList.getUser().then(res => {
            console.log(res)
            if (res.code === 200) {
              for (let i = 0; i < res.userName.length; i++) {
                if (res.userName[i].username === sessionStorage.getItem('loginName')) {
                  next()
                  break
                }
                if (i === res.userName.length - 1) {
                  alert('很抱歉您没有权限访问！')
                }
              }
            }
          })
        }
      },
      children: [{
        path: '/customer/customerList',
        component: () => import('@/views/customer')
      }]
    },
    {
      path: '/order',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          interfaceList.getUser().then(res => {
            console.log(res)
            if (res.code === 200) {
              for (let i = 0; i < res.userName.length; i++) {
                if (res.userName[i].username === sessionStorage.getItem('loginName')) {
                  next()
                  break
                }
                if (i === res.userName.length - 1) {
                  alert('很抱歉您没有权限访问！')
                }
              }
            }
          })
        }
      },
      children: [{
        path: '/order/orderList',
        component: () => import('@/views/order')
      }]
    },
    {
      path: '/staff',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          interfaceList.getUser().then(res => {
            console.log(res)
            if (res.code === 200) {
              for (let i = 0; i < res.userName.length; i++) {
                if (res.userName[i].username === sessionStorage.getItem('loginName')) {
                  next()
                  break
                }
                if (i === res.userName.length - 1) {
                  alert('很抱歉您没有权限访问！')
                }
              }
            }
          })
        }
      },
      children: [{
          path: '/staff/official',
          component: () => import('@/views/staff/officialStaff')
        },
        {
          path: '/staff/temporary',
          component: () => import('@/views/staff/temporaryStaff')
        }
      ]
    },
    {
      path: '/examine',
      component: NavMenu,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('loginStatus') === false && sessionStorage.getItem('loginName') === '') {
          MessageBox('很抱歉您还未登录！请先登录！', '警告', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            next('/login')
          })
        } else {
          interfaceList.getSuperUserName().then(res => {
            console.log(res)
            if (res.code === 200) {
              for (let i = 0; i < res.userName.length; i++) {
                if (res.userName[i].username === sessionStorage.getItem('loginName')) {
                  next()
                  break
                }
                if (i === res.userName.length - 1) {
                  alert('很抱歉您没有权限访问！')
                }
              }
            }
          })
        }
      },
      children: [{
        path: '/examine/examineCenter',
        component: () => import('@/views/examine/')
      }]
    }
  ]
})


export default router
